var WebTorrent = require('./node_modules/webtorrent')
var rtcConfig = {'iceServers': [
    {'url': 'stun:23.21.150.121','urls': 'stun:23.21.150.121'},
    {'url': 'turn:global.turn.twilio.com:3478?transport=udp','username': 'c3eac1694cb7fe5921b77565385aecbae5c2c0437cd4be0fc364ea5d687778ec','credential': '7jdGrLmQmF2y7F++JROgGoSRn25vu/R+Hi6muTfXRwQ=','urls': 'turn:global.turn.twilio.com:3478?transport=udp'},
    {'url': 'turn:global.turn.twilio.com:3478?transport=tcp','username': 'c3eac1694cb7fe5921b77565385aecbae5c2c0437cd4be0fc364ea5d687778ec','credential': '7jdGrLmQmF2y7F++JROgGoSRn25vu/R+Hi6muTfXRwQ=','urls': 'turn:global.turn.twilio.com:3478?transport=tcp'},
    {'url': 'turn:global.turn.twilio.com:443?transport=tcp','username': 'c3eac1694cb7fe5921b77565385aecbae5c2c0437cd4be0fc364ea5d687778ec','credential': '7jdGrLmQmF2y7F++JROgGoSRn25vu/R+Hi6muTfXRwQ=','urls': 'turn:global.turn.twilio.com:443?transport=tcp'}
]}

var opt = {
  dht: true, // Enable DHT (default=true), or options object for DHT
  maxConns: 55, // Max number of connections per torrent (default=55)
  // nodeId: String|Buffer,   // DHT protocol node ID (default=randomly generated)
  // peerId: String|Buffer,   // Wire protocol peer ID (default=randomly generated)
  tracker: {
    rtcConfig: rtcConfig
  }
}
opt.tracker = {rtcConfig: {iceServers: [
  {
    'url': 'ws://rssmv.com:9800',
    'urls': 'ws://rssmv.com:9800'
  }
]}}
var client = new WebTorrent({dht: false,maxConns: 6})

var filePath = './pmy.mp4'

var copt = {
  announce: [
    'ws://rssmv.com:9800'
  ], // Torrent trackers to use (added to list in .torrent or magnet uri)
  // getAnnounceOpts: function(a,b,c){
  //    console.log('getAnnounceOpts',a,b,c)
  //  return true
  // }, // Custom callback to allow sending extra parameters to the tracker
  maxWebConns: 2, // Max number of simultaneous connections per web seed [default=4]
  path: '/webtorrent/', // Folder to download files to (default=`/tmp/webtorrent/`)
/*store: function(a,b,c){
    console.log('store',a,b,c)
} */ // Custom chunk store (must follow [abstract-chunk-store](https://www.npmjs.com/package/abstract-chunk-store) API)
}

client.seed(filePath, copt, function (torrent) {
  console.log('torrentId (info hash):', torrent.infoHash)
  // console.log('torrentId (magnet link):', torrent.magnetURI)
  torrent.on('done', function (err) {
    console.log('torrent download finished')
  })
  torrent.on('error', function (err) {
    console.log(err)
  })
})

client.on('warning', function (err) {
  console.log(err)
})
client.on('error', function (err) {
  console.log(err)
})
